function HtmlFileUploader(uploaderId) {
	this.uploaderId = uploaderId;
	this.objectId = null;
	this.objectType = null;
	this.beanId = null;
	
	this.hiddenObj = document.getElementById(uploaderId);
	this.selectObj = document.getElementById(uploaderId + ".list");
	this.buttonUploadObj = document.getElementById(uploaderId + ".button.upload");
	this.buttonRemoveObj = document.getElementById(uploaderId + ".button.remove");
	this.buttonDownloadObj = document.getElementById(uploaderId + ".button.download");
	
	this.url = "/fileupload";
	if (this.hiddenObj.getAttribute("url") != null)
		this.url = this.hiddenObj.getAttribute("url");
	this.valueProperty = this.hiddenObj.getAttribute("valueProperty");
	this.textProperty = this.hiddenObj.getAttribute("textProperty");
	
	if (this.hiddenObj.getAttribute("objectId") != null && this.hiddenObj.getAttribute("objectId").length != 0)
		this.objectId = this.hiddenObj.getAttribute("objectId");

	if (this.hiddenObj.getAttribute("objectType") != null && this.hiddenObj.getAttribute("objectType").length != 0)
		this.objectType = this.hiddenObj.getAttribute("objectType");
	
	if (this.hiddenObj.getAttribute("beanId") != null && this.hiddenObj.getAttribute("beanId").length != 0)
		this.beanId = this.hiddenObj.getAttribute("beanId");
	
	var bindURL = globeWebAppContext + "/brick/uploadfile.jsp?uploaderId=" + this.uploaderId;	
	if (this.objectId != null) bindURL += "&objectId=" + this.objectId;
	if (this.objectType != null) bindURL += "&objectType=" + this.objectType;
	if (this.beanId != null) bindURL += "&beanId=" + this.beanId;
	
	
	//事件
	var oThis = this;
	if (this.buttonUploadObj && this.buttonUploadObj != null) {
		this.buttonUploadObj.onclick = function () {
			openDialog(bindURL, 400, 200);
		}
	}
	
	if (this.buttonRemoveObj && this.buttonRemoveObj != null) {
		this.buttonRemoveObj.onclick = function () {
			oThis.removeFile();
		}
	}
	
	this.buttonDownloadObj.onclick = function () {
		oThis.downloadFile();
	}
	
	//初始化
	this.listFile();
}

HtmlFileUploader.prototype.downloadFile = function () {
	var fileId = this.selectObj.value;
	if (fileId && fileId != '') {
		var u = this.url + "/download" + "?globe.fileupload.fileid=" + fileId;
		if (window.dialogArguments) {
			if (!document.getElementById("iframeSelf")) {
				document.body.insertAdjacentHTML("beforeEnd", "<iframe id=\"iframeSelf\" name=\"iframeSelf\" style=\"display:none\"></iframe>");
			}
			buildFormPost(globeWebAppContext + u, "iframeSelf");
		} else
			buildFormPost(globeWebAppContext + u, "_self");
	}
}

HtmlFileUploader.prototype.addFile = function (fileId, fileName) {
	var optionObj = document.createElement("OPTION");
	optionObj.value = fileId;
	optionObj.text = fileName;
				
	if (document.all)
		this.selectObj.add(optionObj);
	else
		this.selectObj.appendChild(optionObj);
		
	this.listToHidden();
}

HtmlFileUploader.prototype.removeFile = function () {
	var fileId = this.selectObj.value;
	if (fileId && fileId != '') {
		var u = this.url + "/remove" + "?globe.fileupload.fileid=" + fileId;
		
		var http = new HttpObject();
		var ret = http.httpGetReturnString(u);
		
		if (ret == 'OK') {
			var optionsObj = this.selectObj.options;
			for (i = 0; i < optionsObj.length; i ++) {
				if (optionsObj.item(i).value == fileId) {
					this.selectObj.removeChild(optionsObj.item(i));
					this.listToHidden();
					break;
				}
			}
		}
	}
	
}

HtmlFileUploader.prototype.listFile = function () {
	if (this.objectId != null) {
		var u = this.url + "/list" + "?globe.fileupload.objectid=" + this.objectId;
		if (this.objectType != null) u += "&globe.fileupload.objecttype=" + this.objectType;
		if (this.beanId != null) u+= "&globe.fileupload.beanid=" + this.beanId;
		
		var http = new HttpObject();
		var ret = http.httpGetReturnMaps(u);
		
		if (isValid(this.valueProperty) && isValid(this.textProperty)) {
			for (var i = 0; i < ret.length; i ++) {
				var v = ret[i][this.valueProperty];
				var t = ret[i][this.textProperty];	
				this.addFile(v, t);
			}
		}
	}
}

HtmlFileUploader.prototype.listToHidden = function () {
	var optionsObj = this.selectObj.options;
	var ret = "";
	for (i = 0; i < optionsObj.length; i ++) {
		ret += "," + optionsObj.item(i).value;
	}
	if (ret == "")
		this.hiddenObj.value = "";
	else
		this.hiddenObj.value = ret.substring(1);
}
